package com.yt.order.provider.service.impl;

import com.yt.core.dao.filter.DynamicSQLFilterImpl;
import com.yt.core.dao.filter.QueryFilter;
import com.yt.core.service.GenericServiceImpl;
import com.yt.order.api.interfaces.InstitutionService;
import com.yt.order.api.model.Institution;
import com.yt.order.provider.dao.InstitutionDao;

import java.util.List;

/**
 * Created by liujunshi on 15-12-30.
 */
public class InstitutionServiceImpl extends GenericServiceImpl<Institution, Long> implements InstitutionService {
	private InstitutionDao dao;

	public InstitutionServiceImpl() {
	}

	public InstitutionServiceImpl(InstitutionDao dao) {
		super(dao);
		this.dao = dao;
	}

	@Override
	public boolean checkInstitutionName(String name, Long id) {
		QueryFilter filter = new DynamicSQLFilterImpl();
		filter.addFilter("Q_name_S_EQ", name);
		if (id != null && id != 0) {
			filter.addFilter("Q_id_L_NEQ", id + "");
		}
		return dao.count(filter) == 0;
	}

	@Override
	public boolean checkInstitutionCode(String code, Long id) {
		QueryFilter filter = new DynamicSQLFilterImpl();
		filter.addFilter("Q_code_S_EQ", code);
		if (id != null && id != 0) {
			filter.addFilter("Q_id_L_NEQ", id + "");
		}
		return dao.count(filter) == 0;
	}

	@Override
	public List<Institution> getIdAndName(boolean includeBranch) {
		QueryFilter filter = new DynamicSQLFilterImpl();
		if (!includeBranch) {
			filter.addFilter("Q_parentId_L_EQ", "0");
		}
		filter.setPagingBean(null);
		filter.setSortField("parentId,pinyin", false);
		return dao.getIdAndName(filter);
	}

	@Override
	public List<Institution> getInstitutionByName(String name) {
		QueryFilter filter = new DynamicSQLFilterImpl();
		filter.addFilter("Q_name_S_EQ", name.trim());
		filter.setPagingBean(null);
		return dao.getIdAndName(filter);
	}
	@Override
	public List<Institution> getBaseInfoByName(String name) {
		QueryFilter filter = new DynamicSQLFilterImpl();
		filter.addFilter("Q_name_S_EQ", name.trim());
		filter.setPagingBean(null);
		return dao.getBaseInfo(filter);
	}
	@Override
	public List<Institution> getInstitutionByIds(String ids) {
		QueryFilter filter = new DynamicSQLFilterImpl();
		filter.addFilter("Q_id_S_IN", ids);
		filter.setPagingBean(null);
		return dao.find(filter);
	}
}
